# -*- coding: utf-8 -*-
import sys

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.Qt import *
from PyQt5.QtGui import *

from client.pages.NewFriendWin import *
from client.pages.MyPart import NewFriendItem
from client.pages.FriendInfoPage import *
from client.pages.GroupInfoPage import *
from client.func.ClientHttp import get_user_info_post, clear_new_friend_post, get_new_friend_post
import client.pages.global_win as gw

root = "./"
img_dir = "client/.cache/img_buf/"


class NewFriendPage(QMainWindow, Ui_NewFriendWin):
    def __init__(self):
        super(NewFriendPage, self).__init__()
        self.setupUi(self)
        self.setWindowFlags(Qt.FramelessWindowHint)
        self.myuid = ""
        self.mode = "user"
        self.user_dicts = []
        self.group_dicts = []
        self.user_list = []
        self.group_list = []
        self.close_button.clicked.connect(self.OnClose)
        self.new_friend_button.clicked.connect(lambda: self.OnChangeMode(self.new_friend_button.objectName()))
        self.new_group_button.clicked.connect(lambda: self.OnChangeMode(self.new_group_button.objectName()))
        self.read_button.clicked.connect(self.OnClear)
        self.listWidget.itemClicked.connect(self.OnItemClicked)

    def OnClear(self):
        clear_new_friend_post(self.myuid, self.mode)
        self.update()

    def OnItemClicked(self, item):
        uid = item.uid
        if item.category == "friend" or item.category == "group_apply":
            if "u" + uid not in gw.get_keys():
                win = FriendInfoPage()
                win.uid = uid
                win.myuid = self.myuid
                gw.set_value("u" + uid, win)
                win.update()
                win.show()
            else:
                gw.get_value("u" + uid).update()
                gw.get_value("u" + uid).show()
        else:
            if "g" + uid not in gw.get_keys():
                win = GroupInfoPage()
                win.myuid = self.myuid
                win.uid = uid
                gw.set_value("g" + uid, win)
                win.update()
                win.show()
            else:
                gw.get_value("g" + uid).update()
                gw.get_value("g" + uid).show()

    def OnChangeMode(self, name):
        if name == "new_friend_button":
            self.mode = "user"
        else:
            self.mode = "group"
        self.update()

    def clear(self):
        self.user_list.clear()
        self.group_list.clear()
        self.listWidget.clear()

    def update(self):
        users, groups = get_new_friend_post(self.myuid)
        self.user_dicts = users
        self.group_dicts = groups
        self.clear()
        if self.mode == "user":
            for dic in self.user_dicts:
                item = NewFriendItem()
                item.myuid = self.myuid
                item.uid = dic["from"]
                item.isSolve = dic["isSolve"]
                if item.isSolve:
                    item.info = u"好友申请已处理"
                else:
                    item.info = u"申请添加为好友"
                item.category = "friend"
                data = get_user_info_post(dic["from"])
                if data:
                    item.name = data[1]
                    item.img_path = root + img_dir + data[2]
                    item.update()
                    self.user_list.append(item)
                    self.listWidget.addItem(item)
                    self.listWidget.setItemWidget(item, item.widget)
        else:
            for dic in self.group_dicts:

                item = NewFriendItem()
                item.myuid = self.myuid
                item.isSolve = dic["isSolve"]
                item.category = dic["type"]
                item.group_uid = dic["group_uid"]
                info = dic["group"]
                if item.isSolve:
                    item.info = u"群聊申请已处理"
                else:
                    if item.category == "group_apply":
                        item.info = u"申请加入"+info
                        item.uid = dic["from"]
                data = get_user_info_post(dic["from"])
                if data:
                    item.name = data[1]
                    item.img_path = root + img_dir + data[2]
                    item.update()
                    self.group_list.append(item)
                    self.listWidget.addItem(item)
                    self.listWidget.setItemWidget(item, item.widget)

    def OnClose(self):
        self.close()